﻿Imports System.Data.OleDb

Public Class ClubDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim clubID As String = Request.Params("ClubID")

        If Not String.IsNullOrEmpty(clubID) Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            oleDbConn.Open()

            Dim selectClubSQL As String = "SELECT ClubName FROM Clubs WHERE ID=@f1"
            Dim selectClubCmd As OleDbCommand = New OleDbCommand(selectClubSQL, oleDbConn)
            selectClubCmd.CommandType = CommandType.Text
            selectClubCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
            Dim selectClubDr As String = selectClubCmd.ExecuteScalar()

            If Not String.IsNullOrEmpty(selectClubDr) Then

                lblClubName.Text = selectClubDr
                selectClubCmd.Dispose()

                If Not IsPostBack Then
                    Dim websiteUser As String

                    If Not String.IsNullOrEmpty(User.Identity.Name) Then
                        websiteUser = User.Identity.Name
                    Else
                        websiteUser = "Anonymous"
                    End If

                    Dim createClubViewSQL As String = "INSERT INTO ClubViews(Username, ClubID_FK, ViewedAt) VALUES (@f1,@f2,@f3)"
                    Dim createClubViewCmd As OleDbCommand = New OleDbCommand(createClubViewSQL, oleDbConn)
                    createClubViewCmd.CommandType = CommandType.Text
                    createClubViewCmd.Parameters.AddWithValue("@f1", websiteUser)
                    createClubViewCmd.Parameters.AddWithValue("@f2", clubID)
                    createClubViewCmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
                    createClubViewCmd.ExecuteNonQuery()
                    createClubViewCmd.Dispose()

                    Dim selectClubViewSQL As String = "SELECT ClubUserViews FROM ClubUserViews WHERE ClubName = @f1"
                    Dim selectClubViewCmd As OleDbCommand = New OleDbCommand(selectClubViewSQL, oleDbConn)
                    selectClubViewCmd.CommandType = CommandType.Text
                    selectClubViewCmd.Parameters.AddWithValue("@f1", lblClubName.Text)
                    Dim selectClubViewDr As Int32 = selectClubViewCmd.ExecuteScalar()
                    lblClubViews.Text = "Views: " & selectClubViewDr
                    selectClubViewCmd.Dispose()

                    'Check already liked club
                    Dim selectClubLikeSQL As String = "SELECT * FROM ClubLikes WHERE Username=@f1 AND ClubID_FK=@f2"
                    Dim selectClubLikeCmd As New OleDbCommand(selectClubLikeSQL, oleDbConn)
                    selectClubLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
                    selectClubLikeCmd.Parameters.AddWithValue("@f2", Request.Params("ClubID"))
                    Dim selectClubLikeDr As OleDbDataReader = selectClubLikeCmd.ExecuteReader()

                    If (selectClubLikeDr.HasRows()) Then
                        btnClubLike.Visible = False
                        btnClubUnlike.Visible = True
                    Else
                        btnClubLike.Visible = True
                        btnClubUnlike.Visible = False
                    End If

                    selectClubLikeCmd.Dispose()

                    Dim selectUserLikeSQL As String = "SELECT ClubUserLikes FROM ClubUserLikes WHERE ClubName = @f1"
                    Dim selectUserLikeCmd As OleDbCommand = New OleDbCommand(selectUserLikeSQL, oleDbConn)
                    selectUserLikeCmd.CommandType = CommandType.Text
                    selectUserLikeCmd.Parameters.AddWithValue("@f1", lblClubName.Text)
                    Dim selectUserLikeDr As Int32 = selectUserLikeCmd.ExecuteScalar()

                    If selectUserLikeDr = 1 And btnClubUnlike.Visible Then
                        lblClubLikes.Text = "You like this"
                    ElseIf selectUserLikeDr = 1 And btnClubLike.Visible Then
                        lblClubLikes.Text = "1 person likes this"
                    ElseIf selectUserLikeDr = 2 And btnClubUnlike.Visible Then
                        lblClubLikes.Text = "You and another person like this"
                    ElseIf selectUserLikeDr = 2 And btnClubLike.Visible Then
                        lblClubLikes.Text = "2 people like this"
                    ElseIf selectUserLikeDr > 2 And btnClubUnlike.Visible Then
                        lblClubLikes.Text = "You and " & (selectUserLikeDr - 1) & " others like this"
                    ElseIf selectUserLikeDr > 2 And btnClubLike.Visible Then
                        lblClubLikes.Text = selectUserLikeDr & " people like this"
                    Else
                        lblClubLikes.Text = "Be the first to like this!"
                    End If

                    selectUserLikeCmd.Dispose()

                    'Check already rated clubs by user
                    Dim selectClubRatingSQL As String = "SELECT Rating FROM ClubRatings WHERE ClubID_FK=@f1 AND Username=@f2"
                    Dim selectClubRatingCmd As OleDbCommand = New OleDbCommand(selectClubRatingSQL, oleDbConn)
                    selectClubRatingCmd.CommandType = CommandType.Text
                    selectClubRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
                    selectClubRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)

                    Dim selectClubRatingDr As Int32 = Convert.ToInt32(selectClubRatingCmd.ExecuteScalar())
                    RatingForClub.CurrentRating = selectClubRatingDr

                    selectClubRatingCmd.Dispose()

                End If

            Else
                Session("UserMessage_Error") = "The specified club does not exist!"
                Response.Redirect("~/ClubList.aspx")
            End If

                oleDbConn.Close()
                oleDbConn.Dispose()

            Else
            Session("UserMessage_Error") = "A club must be specified!"
                Response.Redirect("~/ClubList.aspx")
        End If

        If Not String.IsNullOrEmpty(User.Identity.Name) Then
            pnlRating.Visible = False
            phRatings.Visible = True
        Else
            btnClubLike.Visible = False
            btnClubUnlike.Visible = False
            pnlRating.Visible = True
            phRatings.Visible = False
            pnlClubLikes.Visible = False
        End If
    End Sub

    Protected Sub btnClubPlayers_Click(sender As Object, e As EventArgs) Handles btnClubPlayers.Click
        Response.Redirect("~/PlayerList.aspx?ClubID=" + Request.Params("ClubID"))
    End Sub

    Protected Sub btnBackClubList_Click(sender As Object, e As EventArgs) Handles btnBackClubList.Click
        Response.Redirect("~/ClubList.aspx")
    End Sub

    Protected Sub btnClubLike_Click(sender As Object, e As EventArgs) Handles btnClubLike.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim insertClubLikeSQL As String = "INSERT INTO ClubLikes (Username,ClubID_FK,LikedAt) VALUES (@f1,@f2,@f3)"
        Dim insertClubLikeCmd As New OleDbCommand(insertClubLikeSQL, oleDbConn)
        insertClubLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        insertClubLikeCmd.Parameters.AddWithValue("@f2", Request.Params("ClubID"))
        insertClubLikeCmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
        insertClubLikeCmd.ExecuteNonQuery()
        insertClubLikeCmd.Dispose()

        btnClubLike.Visible = False
        btnClubUnlike.Visible = True

        Dim selectClubLikeSQL As String = "SELECT ClubUserLikes FROM ClubUserLikes WHERE ClubName = @f1"
        Dim selectClubLikeCmd As OleDbCommand = New OleDbCommand(selectClubLikeSQL, oleDbConn)
        selectClubLikeCmd.CommandType = CommandType.Text
        selectClubLikeCmd.Parameters.AddWithValue("@f1", lblClubName.Text)
        Dim selectClubLikeDr As Int32 = selectClubLikeCmd.ExecuteScalar()

        If selectClubLikeDr = 1 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You like this"
        ElseIf selectClubLikeDr = 1 And btnClubLike.Visible Then
            lblClubLikes.Text = "1 person likes this"
        ElseIf selectClubLikeDr = 2 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You and another person like this"
        ElseIf selectClubLikeDr = 2 And btnClubLike.Visible Then
            lblClubLikes.Text = "2 people like this"
        ElseIf selectClubLikeDr > 2 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You and " & (selectClubLikeDr - 1) & " others like this"
        ElseIf selectClubLikeDr > 2 And btnClubLike.Visible Then
            lblClubLikes.Text = selectClubLikeDr & " people like this"
        Else
            lblClubLikes.Text = "Be the first to like this!"
        End If

        selectClubLikeCmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnClubUnlike_Click(sender As Object, e As EventArgs) Handles btnClubUnlike.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim removeClubLikeSQL As String = "DELETE FROM ClubLikes WHERE Username=@f1 AND ClubID_FK=@f2"
        Dim removeClubLikeCmd As New OleDbCommand(removeClubLikeSQL, oleDbConn)
        removeClubLikeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        removeClubLikeCmd.Parameters.AddWithValue("@f2", Request.Params("ClubID"))
        removeClubLikeCmd.ExecuteNonQuery()
        removeClubLikeCmd.Dispose()

        btnClubLike.Visible = True
        btnClubUnlike.Visible = False

        Dim selectUserLikeSQL As String = "SELECT ClubUserLikes FROM ClubUserLikes WHERE ClubName = @f1"
        Dim selectUserLikeCmd As OleDbCommand = New OleDbCommand(selectUserLikeSQL, oleDbConn)
        selectUserLikeCmd.CommandType = CommandType.Text
        selectUserLikeCmd.Parameters.AddWithValue("@f1", lblClubName.Text)
        Dim selectUserLikeDr As Int32 = selectUserLikeCmd.ExecuteScalar()

        If selectUserLikeDr = 1 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You like this"
        ElseIf selectUserLikeDr = 1 And btnClubLike.Visible Then
            lblClubLikes.Text = "1 person likes this"
        ElseIf selectUserLikeDr = 2 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You and another person like this"
        ElseIf selectUserLikeDr = 2 And btnClubLike.Visible Then
            lblClubLikes.Text = "2 people like this"
        ElseIf selectUserLikeDr > 2 And btnClubUnlike.Visible Then
            lblClubLikes.Text = "You and " & (selectUserLikeDr - 1) & " others like this"
        ElseIf selectUserLikeDr > 2 And btnClubLike.Visible Then
            lblClubLikes.Text = selectUserLikeDr & " people like this"
        Else
            lblClubLikes.Text = "Be the first to like this!"
        End If

        selectUserLikeCmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnEditClub_Click(sender As Object, e As EventArgs) Handles btnEditClub.Click
        Response.Redirect("~/EditClub.aspx?ClubID=" + Request.Params("ClubID"))
    End Sub

    Protected Sub RatingForClub_Changed(sender As Object, e As AjaxControlToolkit.RatingEventArgs)
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim removeClubRatingSQL As String = "DELETE FROM ClubRatings WHERE ClubID_FK=@f1 AND Username=@f2"
        Dim removeClubRatingCmd As New OleDbCommand(removeClubRatingSQL, oleDbConn)
        removeClubRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
        removeClubRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        removeClubRatingCmd.ExecuteNonQuery()
        removeClubRatingCmd.Dispose()

        Dim insertClubRatingSQL As String = "INSERT INTO ClubRatings (ClubID_FK,Rating,Username,RatingDateTime) VALUES (@f1,@f2,@f3,@f4)"
        Dim insertClubRatingCmd As New OleDbCommand(insertClubRatingSQL, oleDbConn)
        insertClubRatingCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
        insertClubRatingCmd.Parameters.AddWithValue("@f2", RatingForClub.CurrentRating)
        insertClubRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        insertClubRatingCmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
        insertClubRatingCmd.ExecuteNonQuery()
        hdnLabel.Text = "Thank you for your rating!"
        insertClubRatingCmd.Dispose()

        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btnSignIn_Click(sender As Object, e As EventArgs) Handles btnSignIn.Click
        Response.Redirect("~/Account/Login?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl))
    End Sub

    Protected Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        Response.Redirect("~/Account/Register")
    End Sub

End Class